//
//  ExceptionHandler.m
//  Example
//
//  Created by Jpwang on 16/8/10.
//  Copyright © 2016年 Jpwang. All rights reserved.
//

#import "JPCrashHandler.h"

@implementation JPCrashHandler

/*添加崩溃处理函数*/
+ (void) fetchCrash {
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}

/*在调试窗口输出崩溃信息*/
+ (void)printCrashLog {
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filename = [pathList objectAtIndex:0];
    filename=[filename stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", @"jp_excepiton"]];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    fprintf(stderr, "崩溃日志 -> %s\n", [[NSString stringWithFormat:@"%@",dic] UTF8String]);
}

/*获取崩溃日志*/
+ (id)getCrashLog {
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filename = [pathList objectAtIndex:0];
    filename=[filename stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", @"jp_excepiton"]];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    return dic;
}

/*清除所有崩溃日志*/
+ (void)clearCrashLog {
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filename = [pathList objectAtIndex:0];
    filename = [filename stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", @"jp_excepiton"]];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    if (data != nil) {
        [data removeAllObjects];
    } else {
        return;
    }
    @try {
        BOOL isOK = [data writeToFile:filename atomically:YES];
        NSLog(@"写入文件结果 -> %d",isOK);
    } @catch (NSException *exception) {
        NSLog(@"写入文件exception -> %@",exception.description);
    } @finally {
        
    }
}

/*清除指定崩溃日志*/
+ (void)removeCrashLog:(NSMutableDictionary *)dic {
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filename = [pathList objectAtIndex:0];
    filename = [filename stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", @"jp_excepiton"]];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    if (data != nil) {
        [data removeObjectsForKeys:[dic allKeys]];
    } else {
        return;
    }
    @try {
        BOOL isOK = [data writeToFile:filename atomically:YES];
        NSLog(@"写入文件结果 -> %d",isOK);
    } @catch (NSException *exception) {
        NSLog(@"写入文件exception -> %@",exception.description);
    } @finally {
        
    }
}

/*捕获异常情况*/
void UncaughtExceptionHandler(NSException *exception) {
    //private String time;        // 时间
    //private String appVersion;  // app版本
    //private String model;       // 机型
    //private String deviceId;    // 设备id
    //private String os;          // 系统
    //private String osVersion;   // 系统版本
    //private String info;        // 崩溃信息
    //NSString *model = [Helper getDeviceName];
    //NSString *deviceId = obtainTheUniqueCode();
    NSString *time = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *os = [[UIDevice currentDevice] systemName];
    NSString *osVersion = [[UIDevice currentDevice] systemVersion];
    NSString *info = [NSString stringWithFormat:@"%@ \n %@", [exception reason], [exception callStackSymbols]];
//    NSString *info = [NSString stringWithFormat:@"%@", @"d"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
    [data setObject:time forKey:@"time"];
    [data setObject:appVersion forKey:@"appversion"];
    [data setObject:os forKey:@"os"];
    [data setObject:osVersion forKey:@"osversion"];
    [data setObject:info forKey:@"info"];
    
    NSString *key = [NSString stringWithFormat:@"%f",CFAbsoluteTimeGetCurrent()];
    //写入文件
//    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:data forKey:key];
    NSArray *pathList=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filename = [pathList objectAtIndex:0];
    filename=[filename stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", @"jp_excepiton"]];
    NSMutableDictionary *filedata = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    if (filedata == nil) {
        filedata = [[NSMutableDictionary alloc] init];
    }
    [filedata setObject:data forKey:key];
    @try {
        BOOL isOK = [filedata writeToFile:filename atomically:YES];
        NSLog(@"写入文件结果 -> %@",isOK?@"成功":@"失败");
    } @catch (NSException *exception) {
        NSLog(@"写入文件exception -> %@",exception.description);
    } @finally {
        
    }
    //打印出崩溃信息
    fprintf(stderr, "闪退原因 -> %s\n", [[NSString stringWithFormat:@"%@",exception] UTF8String]);
    fprintf(stderr, "堆栈信息 -> %s\n", [[NSString stringWithFormat:@"%@",[exception callStackSymbols]] UTF8String]);
}

@end


